<template>
  <TeamCom></TeamCom>

  <StationSearchCom></StationSearchCom>

  <el-table
    v-loading="stationListLoading"
    :data="stationList.arr"
    stripe
    highlight-current-row
    max-height="290"
    border
  >
    <el-table-column prop="id" label="站场编号" width="100"></el-table-column>
    <el-table-column prop="name" label="站场名称"></el-table-column>
    <el-table-column prop="teamName" label="所属井队"></el-table-column>
    <el-table-column prop="position" label="站场位置"></el-table-column>
    <el-table-column prop="type" label="类型" width="100"></el-table-column>
    <el-table-column prop="createTime" label="创建时间" width="170"></el-table-column>
    <el-table-column prop="pipelineNum" label="管线数量" width="150">
      <template #default="scope">
        <el-tag type="primary">{{ scope.row.pipelineNum }}</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="操作" :width="hasRole('admin') ? 200 : 150">
      <template #default="scope">
        <el-button v-if="hasRole('admin')" type="primary" @click="preEditStation(scope.row)">
          编辑
        </el-button>
        <el-button type="success" @click="jumpToPipeline(scope.row.name)">跳转到管线</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :total="total"
    :page-sizes="[5, 10, 15, 20]"
    layout="total, sizes, prev, pager, next, jumper"
    :background="true"
    class="pagination"
    @change="getStationList"
  />

  <el-dialog v-model="editStationVisible" title="编辑站场" width="40%">
    <el-form :model="editStationForm" label-width="auto" label-position="right" size="large">
      <el-form-item label="站场名称">
        <el-input v-model="editStationForm.name"></el-input>
      </el-form-item>
      <el-form-item label="站场位置">
        <el-input v-model="editStationForm.position"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-tooltip content="可手动输入并回车" placement="right">
          <el-select
            v-model="editStationForm.type"
            filterable
            allow-create
            default-first-option
            :reserve-keyword="false"
            class="type-select"
          >
            <el-option
              v-for="item in ['加压站', '压缩站', '处理站', '加工站', '中转站']"
              :key="item"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-tooltip>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="editStationVisible = false">取 消</el-button>
      <el-button type="primary" @click="editStation">确 定</el-button>
    </template>
  </el-dialog>
</template>
<script setup name="StationManagePage">
import { onMounted } from 'vue'
import { hasRole } from '../utils/authUtils'
import StationSearchCom from '../components/StationSearchCom.vue'
import TeamCom from '../components/TeamCom.vue'
import { useStation } from '../hooks/useStation'
const {
  stationListLoading,
  stationList,
  editStationVisible,
  editStationForm,
  currentPage,
  pageSize,
  total,
  getStationList,
  preEditStation,
  editStation,
  jumpToPipeline
} = useStation()
onMounted(() => {
  getStationList()
})
</script>
<style lang="less" scoped>
.type-select {
  width: 240px;
}
.pagination {
  margin-top: 10px;
}
</style>
